package km

import (
	"database/sql"
	"time"

	_ "github.com/alexbrainman/odbc"
	"github.com/axgle/mahonia"
)

type PosTVipInfo struct {
	CardID        string
	VipName       string
	CardType      string
	CardStatus    string
	OperDate      time.Time
	VipEndDate    time.Time
	VipConsAmount float64
	VipIntegral   float64
}

//GetPosTVipInfoByCardID 通过id获取
func GetPosTVipInfoByCardID(id string) *PosTVipInfo {
	var ptvi PosTVipInfo

	conn, err := sql.Open("odbc", KMJXCConnStr)
	if err != nil {
		panic(err)
	}
	defer conn.Close()
	stmt, err := conn.Prepare(`SELECT card_id,isnull(vip_name,''),card_type,card_status,oper_date,vip_end_date,vip_cons_amount,vip_integral FROM pos_t_vip_info WHERE ( card_id like ? ) `)
	if err != nil {
		panic(err)
	}
	defer stmt.Close()
	row, err := stmt.Query(id + "%")
	if err != nil {
		panic(err)
	}
	defer row.Close()
	dec := mahonia.NewDecoder("gbk")
	for row.Next() {
		row.Scan(&ptvi.CardID, &ptvi.VipName, &ptvi.CardType, &ptvi.CardStatus, &ptvi.OperDate, &ptvi.VipEndDate, &ptvi.VipConsAmount, &ptvi.VipIntegral)
		ptvi.VipName = dec.ConvertString(ptvi.VipName)
		break
	}

	return &ptvi
}

//todo
// func GetPosTVipInfoByVipName(name string) *PosTVipInfo {
// 	ptvi := PosTVipInfo{}

// 	conn, err := sql.Open("odbc", KMJXCConnStr)
// 	if err != nil {
// 		panic(err)
// 	}
// 	defer conn.Close()
// 	stmt, err := conn.Prepare(`SELECT card_id,isnull(vip_name,''),card_type,card_status,oper_date,vip_end_date,vip_cons_amount,vip_integral FROM pos_t_vip_info WHERE ( vip_name like ? ) `)
// 	if err != nil {
// 		panic(err)
// 	}
// 	defer stmt.Close()
// 	row, _ := stmt.Query(name + "%")
// 	defer row.Close()
// 	dec := mahonia.NewDecoder("gbk")
// 	for row.Next() {
// 		row.Scan(&ptvi.CardID, &ptvi.VipName, &ptvi.CardType, &ptvi.CardStatus, &ptvi.OperDate, &ptvi.VipEndDate, &ptvi.VipConsAmount, &ptvi.VipIntegral)
// 		ptvi.VipName = dec.ConvertString(ptvi.VipName)
// 	}
// 	return &ptvi
// }
